package com.wang.controller;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wang.common.Result;
import com.wang.entity.Book;
import com.wang.entity.Orders;
import com.wang.entity.User;
import com.wang.mapper.BookMapper;
import com.wang.mapper.OrdersMapper;

import com.wang.mapper.UserMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


@RestController
@RequestMapping("/orders")
public class OrderController {
    @Resource
    private OrdersMapper ordersMapper;

    @Resource
    BookMapper bookMapper;
    @Resource
    UserMapper userMapper;

    private final String now = DateUtil.now();

    @PostMapping
    public Result save(@RequestBody Orders orders){

        ordersMapper.insert(orders);
        return Result.success();
    }
    @PostMapping("/deleteBatch")
    public Result<?> deleteBatch(@RequestBody List<Integer> ids){
        ordersMapper.deleteBatchIds(ids);
        return Result.success();
    }
    @PutMapping
    public Result<?> update(@RequestBody Orders Order) {
        ordersMapper.updateById(Order);
        return Result.success();
    }
    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id) {
        ordersMapper.deleteById(id);
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result<?> getById(@PathVariable Long id) {
        return Result.success(ordersMapper.selectById(id));
    }

    @PostMapping("/{bookId}/{UserId}")
    public Result<?> createOrder(@PathVariable Long bookId,@PathVariable Long UserId) {
        Book book = bookMapper.selectById(bookId);
        User user = userMapper.selectById(UserId);
        String orderNo = IdUtil.getSnowflake().nextIdStr();
//        String payUrl = "http://localhost:9090/alipay/pay?subject=" + book.getName() + "&no=" + orderNo + "&total=" + book.getPrice();


        Orders order = new Orders();
        order.setName(book.getName());
        order.setNo(orderNo);
        order.setTotal(book.getPrice());
        order.setTime(now);
        order.setUserName(user.getUsername());
        save(order);
        // 新建订单，扣减库存
        return Result.success();
    }
    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search) {
        LambdaQueryWrapper<Orders> wrapper = Wrappers.lambdaQuery();
        if (StrUtil.isNotBlank(search)) {
            wrapper.like(Orders::getNo, search);
        }
        //        Page<User> userPage=userMapper.selectPage(new Page<>(pageNum,pageSize),wrapper );
//        return Result.success(userPage);
        Page<Orders> OrdersPage = ordersMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(OrdersPage);
    }
}
